داستان آبیدیک

customer repository


فارسی

1 کامپیوتر و شبکه:: customer repository

So the next step is to make this interface right here and as I said earlier we're going to add this interface inside the domain service so I add a new file right here and instead of a class you have to pick interface so let's just make the interface right here and let's call it customer repository and actually when you make interfaces its dotnet standard that we put an I in front of it to kind of explain that this is an interface let me just try and create this guy and explain the different names right here so you have the I in front of it and that makes me as a developer avoid aware that this is an interface pretty much meaning that I cannot just do a new keyword in front of an interface it's an eye customer meaning that this is probably something Do with storing working with customers right and it's a repository that pretty much means that it's the repository pattern we've used right here again it's again when you know the words from the software environment it makes sense okay this one is public and interfaces are because I want to be able to use this anywhere also outside of this project right here so that's why it's a public interface good next we do is we kind of need to figure out what we want to implement in this interface now notice let's just grab these create read update delete let's just actually grab Everything here and put it into our customer repo right here now first let's figure out how do we create data well to Create data I need to make some kind of Function that can create data but again this is an interface so I'm not going to actually implement or create how we actually make a new customer in a data Source I'm actually going to instead just explain I'm going to create something right and I'm going to create Something and I need to send in the Customer that I want to create right here right so this is pretty much how I can explain it now there are a few things we can do here first of all of Course we need to import the customer else it doesn't know what a customer is so we get that again from this package Right here so now we have the customer and again ctrl or command dot depending On your setup with your shortcut keys so now we have the customer right here he sent in and when I'm done I've just created the data right but I think a good way of doing in the is pretty much sending the customer back so you have him as a return statement Right here the reason is that normally when you create a customer he comes in without an ID so remember when you send in the customer he'll probably he won't have an ID no ID when enter but he will have an ID when he exits right so a lot Of times I think that is pretty nice to Kind of return the customer athlete's been created even though the only thing that happens is he gets a new ID now again it's up to you can also just return void right here to say you don't want to return the customer when he's Done we also need to somehow be able to read data now the easiest thing we can Read is a single customer right so we can say customer and we'll say read by ID just to explain that we're going to read this customer by the way my B key doesn't work all the time so you'll have to live with that and we'll pass in an ID and then hopefully we'll get a customer back notice how simple that is again I don't do the implementation I just explained the contract the next thing I'll do is I'll also add one that can read all customers now let's just Start out by putting all these customers inside a list which is a generic guy that we already used the static list that we used in the last series and again let's just import this collection generic because that's the namespace that the list is coming from so I can actually see right here this is from system collection so this is a Microsoft class right here that they made for me sweet and let's just say how do we do that well let's just call it read all that's just meaning that you have to implement something if you want to use my interface call read all so you can get all customers back we also have an Update how do we update a customer well Again is up to you but I'm going to Return the customer after he's updated I'm not going to cost me anything so I'll just do that and then I'm going to pass in a customer containing all the information so this is the customer Update right so this is going to be the Customer that I pass in from the outside and then at some point when it's done updating the data source it's going to Return that updated customer for me the last thing that I want to do is I want to be able to delete a customer and again I'm just going to do it like this I'm going to say pass in instance your value with the customer that you want to delete the idea of the guy and then you are golden that's all I have to do now I actually have a customer repository ready to use pretty Much meaning that the interface the contract is ready to use so I can actually work I can keep Working now with my application right here and I can actually also right now If we wanted to I could move my code down to the infrastructure and start Adding the static list that we had Earlier or some kind of fake database let's actually try and do that in the next lesson so you guys can see us using this customer repository right here see You next time have fun. welcome back so now we have our interface the next step is going to be Creating the first infrastructure or the actual implementation on how we do all of this how do we create a customer how do we how do we read all our customers Back how do we actually work with data And that's the goal of this lesson right here we're going to start out by in the infrastructure folder right so if I go back to the drawing right here you'll Notice that now I'm going to start Working down here what we actually Implement our different information Right so now we've created this guy and we've made the interface for the domain service now we're going to try and use it and actually create something so that we can start working with data and Actually storing it let's just try and add a new project right here and let's Just create this time it's going to be dotnet specific okay so it's going to be Done at core specific sorry so what I'm going to do is I'm going to pick the Class library this time but notice I'm not picking the standard library it's the class library if you can't find that Going to dub net core library and there's the class library I'm going to Pick that one I'm going to say next going to keep that in a Core 2.1 next so I'm going to call this customer app infrastructure static data because this is going to contain a static list right later on I'll make a new one called data that we're going to build the realist curl solution in ok so now we have a new Project I'm going to add this and it's Inside infrastructure right here let's Just get rid of this class right here Just to that's just annoying there we go so now we have our new project right here and in there we're going to make a new folder right away so I'll add a new folder and that's going to be called the repositories because that's going to keep contain all repositories in my Solution now we only probably only going To end up with a customer for now but let's say you also had order lines whatever you had that's going to be available in here as well so now we have that access up and running as well and then what we can do is create our first Repository right here called customer repository so I'll just make a class Customer or a Parsi Tori there we go and notice this time that it's an empty class not an interface and it's called customer repository there we go let's Just keep the constructor for now one Very important thing is that right out of the box I'm going you just add i customer repository right here my customer notice I can't find it i customer repository now why can't I Find it you guys should know that now yes you're right of course I didn't add The dependency so I'll just edit reference or at dependencies and say I Need the core and I need the entity down here I need both of them because I need the core to kind of get the I repository and I need the entity to kind of get the customer entity right I'm going to need both there we go so now I can do a command dot and it'll pop up hopefully and tell me if I don't want to use the beautiful domain service right here there we go now that one is imported let's just get rid of the reusing for now we can import it again if we need it sweet it still complaints and that's because even though I call myself now I use the contract customer repository I haven't actually implemented the Functions that I need to call myself again remember this is a contract so to Be able to use the interface customer repository and call yourself a customer repository you need to have all these functions right so we need to now go from actually having an interface or a contract inter actually having an Implementation so you can right click Right here and do the quick fix if you want to and that should be pretty much the same thing with the command dot do you want to implement the interface and I'll say yes and boom I get all the Functions in here but notice they all Just throw an exception right now saying I'm not implemented I'm not implemented and neither am I blah so none of them are implemented let's just move move the delete one to the bottom so it's kind of the crud set up right here there we go so this was all I had to do now I've implemented my new customer Repository right but well I'm not quite there yet because I actually of course need to start implementing all of these different methods right it's not enough Just to have the repository but now it's available and we start using it later on sweet so how do we actually do this well we need to of course start looking into our program and since we're going to make a complete copy of what we already Have just in a new layer we need some kind of static list and a counter and stuff like that to kind of start working with our set up right here so let's just stop by coming these two guys and adding them inside our new repository because that is going to be the fake database for now right it's going to be a local list that represents a list of customers and some kind of ID that can count up right so it's still being a fake in-memory database that as soon as I restart the program it'll be reset kind of sucky but it's a way just to kind of explain to you guys how we can make our first customer repository with a fake data set later on we can easily change this and we will but for now let's just make it simple sweet next lesson what I want to do is I want to start adding these create read update and delete just like we have them over here in the program so I'll start using moving the storage of the domain logic on how to work and save these guys from the program and slowly start moving them into the repository so let's do that in the next lesson see you next time have fun. welcome back so now that we have our different methods here that are not implemented we need to start Implementing those methods now before we do that I want to kind of since we are now working with an internal thing right Here I'm going to make this guy private meaning that people from the outside cannot access our lists directly there we go and then I'll make this an Underscore because in the.net language when we have a private variable we normally make an underscore for it now this pretty much means that now I've encapsulated the list of customers on my Database of customers inside the customer repository so nobody can get it unless they go through my different Functions right here sweet so now we Have that available now how do we create a customer well if we go back to the Program and we have a look at how we Actually create a program a customer Here there's one called add customer right here the way we do it is pretty much just say add the customer right because here we have a list so that's very simple and then we add a new Customer and just add the three properties we have available right now and then we add an ID to the guy now that's a bit different in the new version we have right here because I want to be able to add a customer but I don't want people from the outside to set the ID because I have to be in Control of that so deciding what the ID should be that's something from me to decide in here ok so when I create a Customer I expect that the guy who sends in the customer to me he's actually going to have either no ID or something that I can override so I'm going to say the customers ID is going to be the new ID right here and then I'm just going to add to that ID right so now I have added So that the ID is 1 plus whatever customer I'm at right now in my system suite the next thing I want to do is kind of just say the customer needs to be added to my list so I can just add the customer right here customers start at and then I'll add the customer there we go now we've added the customer and the last thing I do is just return the customer again just to kind of uphold The contract from the repository the customer repository the interface right I have to return a customer in the end there we go so now that should be added and returned happy days sweet so that was a create function we return a list of all the customers That we have available pretty easy we'll just to returns them right here and say we want to return the Customers list so now you have all the customers available that you can use that them for something right how do we get a specific customer again if I jump Back and forth between the program they should be want on how to read a single customer somewhere yeah we go find Customer by ID something like this we Run over the list of customers and look for a specific one let's just grab that one just for now we're just going to do something as simple as that and say somebody's passing in an ID right here I'm going to run over my list of Customers and if I find the customer and he matches the ID I'm going to return that customer if not I'm going to return no right pretty much the same ideas we did in the UI to update a customer now this is a bit more tricky because I'm actually going to remove this from repository later so let me just add a note here so you guys know that it's going to be removed later remove later when we use unit of work oh yeah when That pops up we're going to remove this guy from the repository I'll explain that later but for now we need to be Able to update a customer how do we do that in the program so let's jump back to this file right here do we have an edit yeah we do so we first found the customer that we want to Edit and then we can just set the information on that customer we can do the same thing right here so let's just copy this guy over say we wanna in the repository read the customer so let's just make a local variable right here customer from DB equal notice the bee man I need a new B key I can say this Read write read by ID and then I can pass in the customer to updates ID right and then hopefully let me just give you guys some more space here so you can see what I'm actually typing there we go so now that we have the customer we can just do a null check if customer from DB is actually there not null then let's do Something but if he isn't and we'll just return right here no we didn't update the customer that's kind of the way that I want to explain that we did find him if that's not even all they would go so now we kind of have a customer from the database and now we Just want to update all the information so we'll say custom from database dot first-name equals the customer that we are sending in from the outsides first-name etcetera right let's just do that there we go now we have all the different properties we want to update whoops it's only a single equal sign and when we're done we'll just return the actual customer we just update it there we go so now that should pretty much be The same idea that we had inside the program right we get the customer from The database or from our customers list we update the customer and then we return them when we're done right so now he we have the returned customer and we Get in all if we somehow couldn't find the customer we could pop up saying cosmonaut found I don't know sweet the last one is to delete one and again let's jump back to the program and see how we handled that in the program delete customer we find him and then remove him from the customer list we can use this again so let's just try and do the same thing in our implementation in the repository there we go find a Customer by ID so and that's not quite he thing we're doing here we say Reap ID instead put in the ID that we want to read if the customer is found then we Take him and remove him from the list that we have locally there we go and now we've removed him and then in our case we want to kind of return null if we didn't find the customer all will return the customer that we just deleted I know this might be overkill but that's really up you how you want to do it so now we kind of made an implementation of the same information that we had in our Program but now when we're done next Lesson we're going to remove the domain Logic the service that kind of takes care of storing the data we're going to Remove that from the UI or the program into the domain service layer or inside the core combined with the Infrastructure okay so that's going to be the next lesson so see you next time we'll try and get rid of all domain-specific or infrastructure specific information in here ever so next step is going to be getting rid of the list in the axial view right here or the UI and get that list and start using the one from the customer repository now to do that for now I'm going to break one of the very important things that we use the user interface should not know the infrastructure should I have direct access to it it's okay to use dependency injection but for now we're just going to have direct Access between these two guys and that means that we're going to have the hard line right here instead of the dotted line that you see so we're going to have direct access don't worry we'll fix that later but for now what I'm going to do is I'm going to remove these two guys because I'm not allowed anymore to have my own local list I want the view of the program to Actually start using the list from the Repository how do we do that well first of all we need dependencies we need to know about these projects so we need to know both about the core project to start using the interface here and we Need to about the customer static data to know about the repository implementation down here so I'll just add those two and as again you'll notice that means that now we have access to this guy the core and the UI also have access to this guy which is kind of Not the perfect scenario but I'll get back to that later the next thing I need to do is kind of make a static because we only have one file right here we're Going to make a static I customer repository right here there we go that's going to be just be called customer repository and let's just implement that or get that using statement in here from the domain service and then the first thing I want To do when I start my main for now is just say that customer repository is going to be equal to a new customer Repository and that new customer repository is then from what we call the infrastructure layer down here yep and we need to parent these and we need to get rid of that it's so helpful sometimes the helpfulness is just too much and we'll import that now notice the interface is from up here in the domain service and the implementation is from down here right now that's just the way we do it so now we have a customer repository available and that means that every place inside our system where we right Now are using the old customers list that we had before we need to now change That into actually using the customer Repository and letting the back sorry the database take care of figuring out or the datasource take care of figuring out how to store an actual customer let's just get rid of this ID right here because I shouldn't be allowed to take Care of that that should be my data source and then instead of adding directly to the customers list that we have before we're going to use the create function that we made inside a Repository and then we're going to add this customer one right here there we go that was all we had to do and let's just Get rid of this customer right here now Again let's do it again create a Customer down here again I'm just reusing the code we already have Available so creating that customer right here we're not allowed to put in an ID and then let's also create him inside our new database right here now

واژگان شبکه مترجمین ایران


معنی‌های پیشنهادی کاربران

نام و نام خانوادگی
شماره تلفن همراه
متن معنی یا پیشنهاد شما
Captcha Code